/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon
 												
Class							:				BuildingCompleteCondition
 
Function						: 				Provides a class for holding 
												a building complete condition
****************************************************************************/
package planninglanguage.softconditions;

import base.WargusGameState;
import base.WargusPlayer;
import base.WargusStateImporter;
import base.WargusUnit;


public class BuildingCompleteSoftCondition extends SoftCondition {

	int m_playerID;
	int m_unitID;
	String m_unitType;
	int m_x;
	int m_y;
	
	public BuildingCompleteSoftCondition(String a_buildingType,int a_x,int a_y,int a_playerID)
	{
		m_playerID = a_playerID;
		m_unitID = -1;
		m_unitType = a_buildingType;
		m_x = a_x;
		m_y = a_y;
	}	

	public BuildingCompleteSoftCondition(int unitID)
	{
		m_playerID = -1;
		m_unitID = unitID;
		m_unitType = null;
		m_x = -1;
		m_y = -1;
	}	
	public String toString(int tabs) {
		String n = super.toString(tabs);
		n = n+ "Player Id is:" + m_playerID;
		n = n + "Unit Id is" + m_unitID;
		n = n + "Unit Type is" + m_unitType;
		n = n + "X coordinate is" + m_x;
		n = n + "Y coordinate is" + m_y;
		
		return n;
	}	
	public float test(WargusGameState a_gameState)
	{
		float bestmatch=0.0f;
		WargusPlayer p = a_gameState.getPlayer(m_playerID);
		
//		System.out.println("BuildingCompleteCondition: test(player = " + m_playerID + " , unitType = " + m_unitType + " X = " + m_x + " , Y = " + m_y);
		
		if (p!=null) {
			for(WargusUnit unit:p.getUnits()) {
	
				if (m_unitType == null) {
					if (unit.getUnitID() == m_unitID) {
						float tmp = ((float)(unit.getHitPoints()) / ((float)(WargusStateImporter.unitMaxHitPoints(unit.getType()))));
						if (tmp>bestmatch) bestmatch = tmp;
					} // if 

				} else {
					if (WargusStateImporter.unitTypeToString(unit.getType()).equals(m_unitType) &&
						unit.getLocX() == m_x &&
						unit.getLocY() == m_y) {
						float tmp = ((float)(unit.getHitPoints()) / ((float)(WargusStateImporter.unitMaxHitPoints(unit.getType()))));
						if (tmp>bestmatch) bestmatch = tmp;						
					}
				}
			}
			
		}
		return bestmatch;
	}
	
}
